// pages/myself/send/ChooseStore.js
let network = null;
let utils = null;
let api = null;
const app = getApp();
var Zan = require('../../../dist/index.js');
let WxNotificationCenter = require('../../../utils/WxNotificationCenter.js');
Page(Object.assign({}, Zan.TopTips,{

  /**
   * 页面的初始数据
   */
  data: {
    items: [],
    all_selected:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();
    this.setData({
      BrandId: options.BrandId,
      selected: JSON.parse(options.selected)
    })
    network._post_json(this, api.myStoreList, { brandId: this.data.BrandId }, success);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
   /**
   * 选门店
   */
  selected:function(e){
    var index = e.currentTarget.dataset.index;
    this.data.items[index].selected = !this.data.items[index].selected;
    this.setData({
      items: this.data.items
    });
    isAllSelected(this, this.data.items);
  },

  /**
   * 门店全选
   */
  allSelected: function (e) {
    this.setData({
      all_selected: !this.data.all_selected,
    });

    for (var i = 0; i < this.data.items.length; i++) {
      if (this.data.all_selected) {
        this.data.items[i].selected = true;
       
      } else {
        this.data.items[i].selected = false;

      }
    }
    this.setData({
      items: this.data.items
    });
    isAllSelected(this, this.data.items);
  },

  /**
   * 确认选择
   */
  confirm:function(e){
    var storeIds = "";
    for (var i = 0; i < this.data.items.length;i++){
      if (this.data.items[i].selected){
        var selectedItems = this.data.items[i];
        storeIds += selectedItems.id + ","
        
      }
    }
    if (storeIds.length > 0) {
      storeIds = storeIds.substr(0, storeIds.length - 1);
    }
    this.setData({
      storeIds: storeIds
    })
    if (storeIds.length==0){
      this.showZanTopTips(this.prompt, '请选择门店');
    }else{
      wx.setStorageSync("selected_stores", this.data.storeIds);
      WxNotificationCenter.postNotificationName("RefreshNotification");
      wx.navigateBack();
    }
  }

}))

/**
 * 判断全选
 */
var isAllSelected = function (that, items) {
  var count = 0;
  for (var i = 0; i < that.data.items.length; i++) {
    if (that.data.items[i].selected) {
      count++;
    }
  }
  that.setData({
    count: count
  })
  if (that.data.items.length == count) {
    that.setData({
      all_selected: true
    })
  } else {
    that.setData({
      all_selected: false
    })
  }
}

var success = function(that,res){
  if (res.data.data) {
    console.log(that.data.selected)
    var count = 0;
    for (let i = 0; i < res.data.data.length; i++) {
      res.data.data[i].selected = false;
      for (let j = 0; j < that.data.selected.length; j++) {
        if(res.data.data[i].id == that.data.selected[j]){
          count++;
          res.data.data[i].selected = true;
        }
      }
      res.data.data[i].openTime = utils.formatDate03(new Date(res.data.data[i].openTime));
      res.data.data[i].closeTime = utils.formatDate03(new Date(res.data.data[i].closeTime));
    }
    if (res.data.data.length == count) {
      that.setData({
        all_selected: true
      })
    } else {
      that.setData({
        all_selected: false
      })
    }

    that.setData({
      items: res.data.data
    })
  }
}